// components/GBTEditList/GBTEditList.js
const util = require("../../utils/util.js");
const getdata = require("../../tool/GetDate.js");
var app = getApp();
var itemIndex = 0;

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    dataList: {
      type: JSON,
      value: []
    },
    title: {
      type: String,
      value: ""
    },
    companyTitle: {
      type: String,
      value: ""
    },
    numberTitle: {
      type: String,
      value: ""
    },
    nameTitle: {
      type: String,
      value: ""
    },
    locationTitle: {
      type: String,
      value: ""
    },
    priceTitle: {
      type: String,
      value: ""
    },
    mobileTitle: {
      type: String,
      value: ""
    },
    hallTitle: {
      type: String,
      value: ""
    },
    mealTitle: {
      type: String,
      value: ""
    },
    dateTitle: {
      type: String,
      value: ""
    },
    qtyTitle: {
      type: String,
      value: ""
    },
    familyPriceTitle: {
      type: String,
      value: ""
    },
    mealType: {
      type: Number,
      value: 0
    },
    remarkTitle: {
      type: String,
      value: ""
    },
    codeTitle: {
      type: String,
      value: ""
    },
    enableTitle: {
      type: String,
      value: ""
    },
    timeTitle: {
      type: String,
      value: ""
    },
    attributeTitle: {
      type: String,
      value: ""
    },
    attributeArray: {
      type: JSON,
      value: []
    },
    objectAttributeArray: {
      type: JSON,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    dataList: [],
    companyTitle: "",
    numberTitle: "",
    nameTitle: "",
    locationTitle: "",
    priceTitle: "",
    mobileTitle: "",
    hallTitle: "",
    mealTitle: "",
    dateTitle: "",
    qtyTitle: "",
    familyPriceTitle: "",
    remarkTitle: "",
    codeTitle: "",
    hallArray: [],
    objectHallArray: [],
    hallIndex: 0,
    mealArray: [],
    objectMealArray: [],
    mealIndex: 0,
    date: "",
    startDate: "",
    endDate: "",
    mealType: 0,
    companyArray: [],
    enableTitle: "",
    timeTitle: "",
    attributeArray: [],
    objectAttributeArray: [],
    hallID: 0,
    attributeTitle: "",
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {
      let that = this;
      if (that.data.hallTitle) {
        getdata.getdata({
          url: "gbt/BasicInfo/DiningHall/",
          data: app.globalData.myUserData.districtID,
          success: function (res) {
            console.log(res.root);
            if (res.root) {
              // let diningHallIndex = wx.getStorageSync("diningHallPickerIndex");
              // if (!diningHallIndex) {
              //   diningHallIndex = 0;
              // }

              that.setData({
                hallArray: res.root.diningHallTitleArray,
                objectHallArray: res.root.diningHallArray,
                // index: diningHallIndex,
              });
            }
          },
        });
      }
      if (that.data.mealTitle) {
        getdata.getdata({
          url: "gbt/BasicInfo/MealList/",
          data: app.globalData.myUserData.districtID,
          success: function (res) {
            console.log(res.root);
            if (res.root) {
              let array = [];
              for (let mealIndex = 0; mealIndex < res.root.data.length; mealIndex++) {
                array.push(res.root.data[mealIndex].name);
              }

              that.setData({
                mealArray: array,
                objectMealArray: res.root.data,
              });
            }
          }
        });
      }

      if (app.globalData.basic_list.length > 0) {
        let list = this.data.dataList;
        console.log(list);
        console.log(app.globalData.basic_list);
        list[itemIndex].companyID = app.globalData.basic_list[app.globalData.basic_index].id;
        list[itemIndex].companyNumber = app.globalData.basic_list[app.globalData.basic_index].number;
        list[itemIndex].companyName = app.globalData.basic_list[app.globalData.basic_index].name;
        this.setData({
          dataList: list,
          companyArray: app.globalData.basic_list,
        });
        app.globalData.basic_list = [];
      }

      let startDate = "";
      let endDate = "";
      if (this.data.mealType == 2) {
        if (util.formatTime(new Date) < util.formatDate(new Date) + " 16:00:00") {
          startDate = util.formatDateDiff2(1);
        } else {
          startDate = util.formatDateDiff2(2);
        }
      } else if (this.data.mealType == 1) {
        startDate = util.formatDateDiff2(0);
      }
      endDate = util.formatDateDiff2(7);
      that.setData({
        startDate: startDate,
        endDate: endDate,
      });
    },
    hide: function () {

    },
    resize: function () {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onItemAdd: function (e) {
      // console.log(e);
      let data = this.data.dataList;
      let item = {};
      item.state = "add";//服务端数据持久化状态
      item.disabled = false;
      item.btnName = "保存";
      item.userID = app.globalData.myUserData.userID;
      item.districtID = app.globalData.myUserData.districtID;
      item.type = this.data.mealType;
      item.itemState = "add";//仅作操作状态使用
      data.unshift(item);
      this.setData({
        dataList: data,
      });
    },

    onNumberBlur: function (e) {
      console.log(e.currentTarget.dataset);
      let list = this.data.dataList;
      list[e.currentTarget.dataset.index].number = e.detail.value;
      this.setData({
        dataList: list,
      });
    },

    onNameBlur: function (e) {
      console.log(e.currentTarget.dataset);
      let list = this.data.dataList;
      list[e.currentTarget.dataset.index].name = e.detail.value;
      this.setData({
        dataList: list,
      });
    },

    onLocationBlur: function (e) {
      let list = this.data.dataList;
      list[e.currentTarget.dataset.index].location = e.detail.value;
      this.setData({
        dataList: list,
      });
    },

    onItemEdit: function (e) {
      console.log(e.currentTarget.dataset);

      let btnName = this.data.dataList[e.currentTarget.dataset.index].btnName;
      if (btnName === "编辑") {
        let list = this.data.dataList;
        list[e.currentTarget.dataset.index].disabled = false;
        list[e.currentTarget.dataset.index].btnName = "保存";
        list[e.currentTarget.dataset.index].itemState = "modify";
        this.setData({
          dataList: list,
        });
      } else {
        let list = this.data.dataList;
        list[e.currentTarget.dataset.index].disabled = true;
        list[e.currentTarget.dataset.index].btnName = "编辑";
        list[e.currentTarget.dataset.index].itemIndex = e.currentTarget.dataset.index;
        this.setData({
          dataList: list,
        });
        app.globalData.basic_list = [];
        app.globalData.basic_index = -1;
        app.globalData.basic_title = "";
        app.globalData.basic_url = "";
        this.triggerEvent('save', list[e.currentTarget.dataset.index]);
      }
    },

    onItemDelete: function (e) {
      let that = this;
      //删除
      let list = this.data.dataList;
      list[e.currentTarget.dataset.index].disabled = true;
      wx.showModal({
        title: '温馨提示',
        content: `确认删除：“${list[e.currentTarget.dataset.index].name}” ?`,
        success: function (res) {
          if (res.confirm) {
            list[e.currentTarget.dataset.index].disabled = true;
            list[e.currentTarget.dataset.index].btnName = "编辑";
            list[e.currentTarget.dataset.index].state = "del";
            list[e.currentTarget.dataset.index].itemIndex = e.currentTarget.dataset.index;
            that.triggerEvent('del', list[e.currentTarget.dataset.index]);
          }
        }
      })
    },

    onLocation: function (e) {
      let that = this;
      console.log(e.currentTarget.dataset);
      //选择位置
      wx.chooseLocation({
        success: function (res) {
          console.log(res.address);
          let list = that.data.dataList;
          list[e.currentTarget.dataset.index].location = res.address; //获取到选择的位置
          that.setData({
            dataList: list,
          });
        }
      });
    },

    onPriceBlur: function (e) {
      let list = this.data.dataList;
      list[e.currentTarget.dataset.index].price = e.detail.value;
      this.setData({
        dataList: list,
      });
    },

    onMobileBlur: function (e) {
      let list = this.data.dataList;
      list[e.currentTarget.dataset.index].mobile = e.detail.value;
      this.setData({
        dataList: list,
      });
    },

    onRemarkBlur: function (e) {
      let list = this.data.dataList;
      list[e.currentTarget.dataset.index].remark = e.detail.value;
      this.setData({
        dataList: list,
      });
    },

    bindCompanyPickerChange: function (e) {
      console.log(e);
      app.globalData.basic_list = [];
      app.globalData.basic_index = 0;
      app.globalData.basic_url = "gbt/BasicInfo/Company/";
      app.globalData.basic_title = "公司信息";
      itemIndex = e.currentTarget.dataset.index;
      let currentItem = this.data.dataList[itemIndex];
      if (currentItem.state == "add") {
        wx.navigateTo({
          url: '/components/GBTBasicInfo/GBTBasicInfo',
        })
      }
    },

    bindHallPickerChange: function (e) {
      console.log(e);
      let list = this.data.dataList;
      let object = this.data.objectHallArray;

      list[e.currentTarget.dataset.index].hallID = object[e.detail.value].id;
      list[e.currentTarget.dataset.index].hallName = object[e.detail.value].name;
      this.setData({
        dataList: list,
        hallIndex: e.detail.value,
      });
    },

    onMealPickerChange: function (e) {
      let list = this.data.dataList;
      let object = this.data.objectMealArray;

      list[e.currentTarget.dataset.index].mealID = object[e.detail.value].id;
      list[e.currentTarget.dataset.index].mealName = object[e.detail.value].name;
      list[e.currentTarget.dataset.index].familyPrice = object[e.detail.value].familyPrice;
      this.setData({
        dataList: list,
        mealIndex: e.detail.value,
      });
    },

    bindAttributePickerChange:function(e){
      console.log(e);
      let list = this.data.dataList;
      let object = this.data.objectAttributeArray;

      list[e.currentTarget.dataset.index].attributeID = object[e.detail.value].id;
      list[e.currentTarget.dataset.index].attributeName = object[e.detail.value].name;
      list[e.currentTarget.dataset.index].attributeIndex = e.detail.value;
      this.setData({
        dataList: list,
      });
    },

    bindTimeChange: function (e) {
      console.log(e);
      let list = this.data.dataList;
      list[e.currentTarget.dataset.index].deadline = e.detail.value;
      this.setData({
        dataList: list,
      });
    },

    onDateChange: function (e) {
      let list = this.data.dataList;

      list[e.currentTarget.dataset.index].date = e.detail.value;
      this.setData({
        dataList: list,
        date: e.detail.value,
      });
    },

    onQtyBlur: function (e) {
      let list = this.data.dataList;
      list[e.currentTarget.dataset.index].qty = e.detail.value;
      this.setData({
        dataList: list,
      });
    },

    onFamilyPriceBlur: function (e) {
      let list = this.data.dataList;
      list[e.currentTarget.dataset.index].familyPrice = e.detail.value;
      this.setData({
        dataList: list,
      });
    },

    switchChange: function (e) {
      console.log(e);
      let list = this.data.dataList;
      console.log(list);
      list[e.currentTarget.dataset.index].isEnable = e.detail.value;
      this.setData({
        dataList: list,
      });
      this.triggerEvent('enable', list[e.currentTarget.dataset.index]);
    }
  }
})